﻿using Grain.Interfaces;
using Orleans;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Grain.Implements
{
    public class NotifyHandler
    {
        public class NotifyEntity
        {
            public string channel { get; set; }
            public string message { get; set; }
        }

        private List<NotifyEntity> notifys { get; set; }

        private const string NotifySendChannel = "system";

        public NotifyHandler(List<NotifyEntity> notifys)
        {
            this.notifys = notifys;
        }

        public async Task WorkingAsync(IGrainFactory factory)
        {
            foreach (var notify in notifys)
            {
                var channel = factory.GetGrain<INotifyService>(notify.channel);
                await channel.Publish(notify.message, NotifySendChannel);
            }
        }
    }
}
